//引入公共函数util
let util = require('../../utils/util');

//获取请求接口的地址
let requestDomain = util.config('requestDomain');

//声明用于存放页面实例的变量
let that;

Page({

  data: {
    
    useMode: 'addGoods',
    goodsId: '',
    goodsName: '',
    goodsPrice: '',
    goodsCount: 1,
    goodsForce: false,
    goodsChecked: false,
    goodsList: []

  },

  onLoad: function (params) {
    that = this;
    //更新操作模式和菜品类型列表
    that.setData({
        useMode: params.useMode
    });
    that.getGoodsList();
  },

  inputGoodsName: function(e) {
      that.data.goodsName = e.detail.value;
  },

  inputGoodsPrice: function(e) {
      that.data.goodsPrice = e.detail.value;
  },

  inputGoodsCount: function(e) {
      that.data.goodsCount = parseInt(e.detail.value);
  },

  checkboxChange: function(e) {
    let checkList = e.detail.value;
    console.log(checkList);
    if (checkList.indexOf('goodsForce') != -1) {
        that.setData({ goodsForce: true, goodsChecked: true });
    }
    else if (checkList.indexOf('goodsChecked') != -1) {
        that.setData({ goodsForce: false, goodsChecked: true  });
    }
    else {
        that.setData({ goodsForce: false, goodsChecked: false });
    }
  },

  addGoods: function() {
      let {goodsName, goodsPrice, goodsCount, goodsForce, goodsChecked, goodsList} = that.data;
    wx.showNavigationBarLoading();
    wx.request({
        url: requestDomain + 'goods/addGoods',
        method: 'POST',
        data: {
            storeId: util.globalData('storeId'),
            _3rdSession: util.globalData('_3rdSession'),
            goodsName: goodsName,
            goodsPrice: goodsPrice,
            goodsCount: goodsCount,
            goodsForce: goodsForce ? 1 : 0,
            goodsChecked: goodsChecked ? 1 : 0
        },
        success: function(result) {
            wx.hideNavigationBarLoading();
            if(util.checkSuccess(result) && result.data && result.data.data) {
                let {goodsId, timestamp} = result.data.data;
                wx.showToast({
                    title: '添加成功',
                    icon: 'success'
                });
                goodsList.push({
                    goodsId: goodsId,
                    goodsName: goodsName,
                    goodsPrice: goodsPrice * 100,
                    goodsCount: goodsCount,
                    goodsForce: goodsForce,
                    goodsChecked: goodsChecked,
                    timestamp: timestamp
                });
                goodsList.sort(function(a, b) {
                    return b.timestamp - a.timestamp;
                });
                that.setData({
                    goodsName: '',
                    goodsPrice: '',
                    goodsCount: 1,
                    goodsForce: false,
                    goodsChecked: false,
                    goodsList: goodsList
                });
            }
            else {
                util.showError('添加失败', '添加附加物品时出现错误' + result.data.errMsg, result.data.errCode);
            }
        },
        fail: function() {
            wx.hideNavigationBarLoading();
            util.showError('添加失败', '发起添加附加物品请求失败');
        }
    });
  },

  editGoods: function() {
    let {goodsList, goodsId, goodsName, goodsPrice, goodsCount, goodsForce, goodsChecked} = that.data;
    console.log(that.data);
    wx.showNavigationBarLoading();
    wx.request({
        url: requestDomain + 'goods/editGoods',
        method: 'POST',
        data: {
            storeId: util.globalData('storeId'),
            _3rdSession: util.globalData('_3rdSession'),
            goodsId: goodsId,
            goodsName: goodsName,
            goodsPrice: goodsPrice,
            goodsCount: goodsCount,
            goodsForce: goodsForce ? 1 : 0,
            goodsChecked: goodsChecked ? 1 : 0
        },
        success: function(result) {
            wx.hideNavigationBarLoading();
            if (util.checkSuccess(result) && result.data && result.data.data) {
                let {timestamp} = result.data.data;
                wx.showToast({
                    title: '修改成功',
                    icon: 'success'
                });
                for (let goodsIndex in goodsList) {
                    if (goodsList[goodsIndex].goodsId == goodsId) {
                        goodsList[goodsIndex] = {
                            goodsId: goodsId,
                            goodsName: goodsName,
                            goodsPrice: goodsPrice * 100,
                            goodsCount: goodsCount,
                            goodsForce: goodsForce,
                            goodsChecked: goodsChecked,
                            timestamp: timestamp
                        }
                        break;
                    }
                }
                goodsList.sort(function (a, b) {
                    return b.timestamp - a.timestamp;
                });
                that.setData({
                    useMode: 'addGoods',
                    goodsId: '',
                    goodsName: '',
                    goodsPrice: '',
                    goodsCount: 1,
                    goodsForce: false,
                    goodsChecked: false,
                    goodsList: goodsList
                 });
            }
            else {
                util.showError('修改失败', '修改附加物品数据失败' + result.data.errMsg, result.data.errCode);
            }
        },
        fail: function() {
            wx.hideNavigationBarLoading();
            util.showError('修改失败', '发起修改附加物品数据请求失败');
        }
    });
  },

  delGoods: function(goodsId) {
    wx.showNavigationBarLoading();
    wx.request({
        url: requestDomain + 'goods/delGoods',
        method: 'POST',
        data: {
            storeId: util.globalData('storeId'),
            _3rdSession: util.globalData('_3rdSession'),
            goodsId: goodsId
        },
        success: function(result) {
            wx.hideNavigationBarLoading();
            if(util.checkSuccess(result) && result.data && result.data.data) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'success'
                });
                let {goodsList} = that.data;
                for (let goodsIndex in goodsList) {
                    if (goodsList[goodsIndex].goodsId == goodsId) {
                        goodsList.splice(goodsIndex, 1);
                        break;
                    }
                }
                goodsList.sort(function (a, b) {
                    return b.timestamp - a.timestamp;
                });
                that.setData({goodsList: goodsList});
            }
            else {
                util.showError('删除失败', '删除附加物品失败' + result.data.errMsg, result.data.errCode);
            }
        },
        fail: function() {
            wx.hideNavigationBarLoading();
            util.showError('删除失败', '发起删除附加物品请求失败');
        }
    })
  },

  cancelEdit: function() {
    that.setData({
        useMode: 'addGoods',
        goodsId: '',
        goodsName: '',
        goodsPrice: '',
        goodsCount: 1,
        goodsForce: false,
        goodsChecked: false
    });
  },

  useGoods: function(e) {
    let goodsId = e.currentTarget.dataset.goodsid;
    wx.showActionSheet({
        itemList: ['修改物品', '删除物品'],
        success: function(result) {
            if(result.tapIndex == 0) {
                let {goodsList} = that.data;
                for(let goodsIndex in goodsList) {
                    if (goodsList[goodsIndex].goodsId == goodsId) {
                        let {goodsName, goodsPrice, goodsCount, goodsForce, goodsChecked} = goodsList[goodsIndex];
                        that.setData({
                            useMode: 'editGoods',
                            goodsId: goodsId,
                            goodsName: goodsName,
                            goodsPrice: goodsPrice / 100,
                            goodsCount: goodsCount,
                            goodsForce: goodsForce,
                            goodsChecked: goodsChecked
                        });
                        break;
                    }
                }
            }
            else if(result.tapIndex == 1) {
                wx.showModal({
                    title: '是否删除？',
                    content: '是否确认删除这个附加物品？',
                    success: function(modal) {
                        if(modal.confirm) {
                            that.delGoods(goodsId);
                        }
                    }
                });
            }
        }
    });
  },

  getGoodsList: function() {
    wx.showNavigationBarLoading();
    wx.request({
        url: requestDomain + 'goods/getGoodsList',
        method: 'POST',
        data: {
            storeId: util.globalData('storeId'),
            _3rdSession: util.globalData('_3rdSession')
        },
        success: function(result) {
            wx.hideNavigationBarLoading();
            if(util.checkSuccess(result) && result.data && result.data.data) {
                let goodsList = result.data.data;
                goodsList.sort(function (a, b) {
                    return b.timestamp - a.timestamp;
                });
                that.setData({ goodsList: goodsList });
            }
            else {
                util.showError('获取失败', '获取附加物品列表失败');
            }
        },
        fail: function() {
            wx.hideNavigationBarLoading();
            util.showError('获取失败', '发起获取附加物品列表请求失败');
        }
    });
  }
});